
package com.gcloud.controller.storage.driver;

import com.gcloud.common.util.SystemUtil;
import com.gcloud.controller.storage.entity.Snapshot;
import com.gcloud.controller.storage.entity.StoragePool;
import com.gcloud.controller.storage.entity.Volume;
import com.gcloud.controller.storage.model.CreateDiskResponse;
import com.gcloud.core.exception.GCloudException;
import com.gcloud.core.messagebus.MessageBus;
import com.gcloud.core.util.MessageUtil;
import com.gcloud.header.compute.enums.StorageType;
import com.gcloud.header.log.enums.LogType;
import com.gcloud.header.storage.StorageErrorCodes;
import com.gcloud.header.storage.msg.node.pool.NodeCreateStoragePoolMsg;
import com.gcloud.header.storage.msg.node.volume.NodeCreateDiskMsg;
import com.gcloud.header.storage.msg.node.volume.NodeDeleteDiskMsg;
import com.gcloud.header.storage.msg.node.volume.NodeResizeDiskMsg;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class LocalStorageDriver implements IStorageDriver {

    @Autowired
    private MessageBus bus;

    @PostConstruct
    private void init() {

    }

    @Override
    public StorageType storageType() {
        return StorageType.FILE;
    }

    @Override
    public void createStoragePool(String poolId, String poolName, String hostname, String taskId) throws GCloudException {
        NodeCreateStoragePoolMsg msg = new NodeCreateStoragePoolMsg();
        msg.setStorageType(this.storageType().getValue());
        msg.setPoolId(poolId);
        msg.setPoolName(poolName);
        msg.setServiceId(MessageUtil.storageServiceId(hostname));
        msg.setTaskId(taskId);
        this.bus.send(msg);
    }

    @Override
    public void deleteStoragePool(String poolName) throws GCloudException {

    }

    @Override
    public CreateDiskResponse createVolume(String taskId, StoragePool pool, Volume volume) throws GCloudException {
        NodeCreateDiskMsg msg = new NodeCreateDiskMsg();
        msg.setStorageType(this.storageType().getValue());
        msg.setVolumeId(volume.getProviderRefId());
        msg.setPoolName(volume.getPoolName());
        msg.setSize(volume.getSize());
        msg.setServiceId(MessageUtil.storageServiceId(pool.getHostname()));
        msg.setTaskId(taskId);
        this.bus.send(msg);

        CreateDiskResponse response = new CreateDiskResponse();
        response.setLogType(LogType.SYNC);
        response.setDiskId(volume.getId());
        return response;
    }

    @Override
    public void deleteVolume(String taskId, StoragePool pool, Volume volume) throws GCloudException {
        NodeDeleteDiskMsg msg = new NodeDeleteDiskMsg();
        msg.setStorageType(this.storageType().getValue());
        msg.setVolumeId(volume.getProviderRefId());
        msg.setPoolName(volume.getPoolName());
        msg.setServiceId(MessageUtil.storageServiceId(pool.getHostname()));
        msg.setTaskId(taskId);
        this.bus.send(msg);
    }

    @Override
    public void resizeVolume(String taskId, StoragePool pool, Volume volume, int newSize) throws GCloudException {
        NodeResizeDiskMsg msg = new NodeResizeDiskMsg();
        msg.setStorageType(this.storageType().getValue());
        msg.setVolumeId(volume.getProviderRefId());
        msg.setPoolName(volume.getPoolName());
        msg.setOldSize(volume.getSize());
        msg.setNewSize(newSize);
        msg.setServiceId(MessageUtil.storageServiceId(pool.getHostname()));
        msg.setTaskId(taskId);
        this.bus.send(msg);
    }

    @Override
    public void createSnapshot(Volume volume, String snapshotRefId) throws GCloudException {

    }

    @Override
    public void deleteSnapshot(Volume volume, Snapshot snapshot) throws GCloudException {

    }

    @Override
    public void resetSnapshot(Volume volume, Snapshot snapshot) throws GCloudException {

    }

}
